package com.huangwenshuo.futurestarschoolmanagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huangwenshuo.futurestarschoolmanagement.exception.CustomRuntimeException;
import com.huangwenshuo.futurestarschoolmanagement.mapper.RolesMapper;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.RolesPo;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.RolesVo;
import com.huangwenshuo.futurestarschoolmanagement.service.RolesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangwenshuo
 * @since 2020-12-26
 */
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, RolesPo> implements RolesService {

    @Autowired
    private RolesMapper rolesMapper;

    @Override
    public void addRole(Integer uid, String role) {
        QueryWrapper q = new QueryWrapper();
        q.eq("role_name",role);
        q.eq("is_delete",0);
        RolesPo rolesPo = rolesMapper.selectOne(q);
        if(rolesPo == null){
            throw new CustomRuntimeException("该职位不存在");
        }
        addUserRole(uid,rolesPo.getRid());
    }

    @Override
    public void addUserRole(Integer uid, Integer rid) {
        rolesMapper.addUserRole(uid,rid);
    }

    @Override
    public List<RolesVo> getRoles(int page, int rows) {
        List<RolesVo>  rolesVo = rolesMapper.getRoles((page-1)*rows,page*rows);
        return rolesVo;
    }

    @Override
    public RolesVo getRolesInfo(Integer rid,int page,int rows) {
        RolesVo rolesVo = rolesMapper.getRolesInfo(rid,(page-1)*rows,page*rows);
        return rolesVo;
    }
}
